package controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.Student;
import service.Studentservice;
import untils.Page;
@Controller
public class StudentController {
	@Autowired
	private Studentservice studentservice;
	public Studentservice getStudentservice() {
		return studentservice;
	}
	public void setStudentservice(Studentservice studentservice) {
		this.studentservice = studentservice;
	}
	
	
	  @RequestMapping("/studentf") 
	  public String student() { 
		  return "redirect:studentfindall"; 
	  }


	/**增加学生信息*/
	
	  @RequestMapping(value = "/studentincrease",method =RequestMethod.POST) 
	  public String increase(Student student, HttpServletRequest request,HttpSession session)  {
		  if(student!=null) {
			  studentservice.increase(student);
		  }
		  return "redirect:studentfindall"; 
	  }
	 
	/** 查看学生信息*/
	@RequestMapping(value = "/studentfind")
	@ResponseBody
	public Student find(@RequestParam int ID) {
		Student s=studentservice.find(ID);
		return s;
	}
	/** 查看学生信息2*/
	@RequestMapping(value = "/studentfindname")
	public String findname(@RequestParam String name,@RequestParam String grade,@RequestParam String major,
			HttpServletRequest request,HttpSession session) {
		Student student=studentservice.findname(name, grade, major);
		request.setAttribute("msg", student.toString());
		return "paper";
	}
	
	/**修改学生信息*/
	@RequestMapping(value = "/studentupdate")
	@ResponseBody
	public String update(Student student, HttpServletRequest request,HttpSession session) {
		if(student!=null) {
			studentservice.update(student);
		  }
		return "redirect:studentfindall";
	}
	
	
	
	/**删除学生信息*/
	@RequestMapping(value = "/studentdelete")
	public String delete(int ID, HttpServletRequest request,HttpSession session) {
		Student student=studentservice.find(ID);
		if(student!=null) {
			studentservice.delete(ID);
			return "ok";
		}
		return null;
	}
	/**
	 *  查询学生列表
	 */
	@RequestMapping(value = "/studentfindall")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, 
			String name, String grade, String major, Model model) {
		// 条件查询所有客户
		Page<Student> students = studentservice.findstudentList(page, rows,name, grade, major);
		model.addAttribute("page",students);
		// 添加参数
		model.addAttribute("name", name);
		model.addAttribute("grade", grade);
		model.addAttribute("major", major);
		return "student";
	}

}
